package com.yxh.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.common.annotation.Authorization;
import com.yxh.pojo.Dept;
import com.yxh.pojo.User;
import com.yxh.service.DeptService;
import com.yxh.service.UserService;
import com.yxh.util.Response;
import com.yxh.util.SnowflakeIdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.yxh.util.APIErrorCode.DELETE_ERROR;
import static com.yxh.util.APIErrorCode.SUCCESS;
import static com.yxh.util.APIErrorCode.INTERNAL_ERROR;
import static com.yxh.util.APIErrorCode.INTERNAL_EXECT_USER_ERROR;

/**
 * Created by asus on 2018/5/31.
 */

@Scope("prototype")
@RestController
@RequestMapping(value = "/dept")
public class DeptController extends com.yxh.base.BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DeptController.class);
    private Response response;

    @Autowired
    private DeptService deptService;

    /**
     * 保存
     *
     * @param gept
     * @return
     */
    @PostMapping("save_dept")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response saveDept(@RequestBody Dept gept) {
        gept.setDeptId(SnowflakeIdWorker.creatId());
        int i = deptService.save(gept);
        if (i == 0) {
            return Response.returnResponse(INTERNAL_ERROR);
        }else if(i ==-1){
            return Response.returnResponse(INTERNAL_EXECT_USER_ERROR);
        }
        else {
            response = Response.returnResponse(SUCCESS);
            response.setData(gept.getDeptId());
            return response;
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response removeDept(@PathVariable("id") String id) {
        deptService.delete(id);
        return Response.returnResponse(SUCCESS);
    }

    /**
     * 修改
     *
     * @param deptJson
     * @return
     */
    @PutMapping("update_deptInfo")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response updateDept(@RequestBody Dept deptJson) {
        int i = deptService.update(deptJson);
        if (i == 0)
            return Response.returnResponse(DELETE_ERROR);
        else
            return Response.returnResponse(SUCCESS);
    }

    /**
     * 查询
     * @param deptJson
     * @return
     */
    @GetMapping("get_information")
    @ResponseBody
    public Response getInformation(Dept deptJson, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = deptService.getInformation(deptJson, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);

        return response;
    }


}
